// pages2/pages/person/personInfo/infoIndex/index.js
import * as Storage from '../../../../../utils/storage';
import { AVATAR_URL, USER_ID } from '../../../../../utils/storage';
const $api = require('../../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    name: '',
    sex: '',
    age: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      avatar: wx.getStorageSync(AVATAR_URL) || '/src/static/default-avatar.png'
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  async getData() {
    const userId = wx.getStorageSync(USER_ID);
    let res = await $api.getInfoDataByUser({ userId });
    res = res.data.data;
    if (res) {
      let { name, sex, age } = res;
      this.setData({
        name,
        sex,
        age
      });
    }
  },

  /**
   * 跳转界面
   */
  handleJumpInterface(e) {
    const { url } = e.currentTarget.dataset;
    const userId = wx.getStorageSync(Storage.USER_ID);
    const accessToken = wx.getStorageSync(Storage.ACCESS_TOKEN);
    const route = encodeURIComponent(`${url}?userId=${userId}&accessToken=${accessToken}`);
    wx.navigateTo({
      url: `/subcontract/person/pages/h5Page/index?url=${route}`
    });
  }
});
